﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HZ.Supervise.Web.Peculiar
{
    public partial class UpdateProcureCatalog : BasePage
    {
        string GID, type;

        HZ.Supervise.IBLL.IBLL_SpecialAuthority sp = HZ.Supervise.BLLFactory.IBLL_SpecialAuthority();
        protected void Page_Load(object sender, EventArgs e)
        {
            GID = Request["GID"];
            type = Request["type"];
            if (!IsPostBack)
            {
                dataBind();
            }
        }
        public UpdateProcureCatalog() 
        {
            this.ModuleNo = "特殊权限";
            this.PageOperateType = "维护";
        }
        private void dataBind()
        {
           
            switch (type)
            {
                case "1":
                    HZ.Supervise.IBLL.IBLL_ProcureCatalog ipro = HZ.Supervise.BLLFactory.IBLL_ProcureCatalog();
                    Model.View.ProcureCatalog procureCatalog = ipro.GetProcureCatalogByID(GID);
                    txtCompany_SC.Text = procureCatalog.CompanyName_SC;
                    txtCompany_TB.Text = procureCatalog.CompanyName_TB;
                    txtFactor.Text = procureCatalog.Factor.ToString();
                    txtGoodsName.Text = procureCatalog.GoodsName;
                    txtMedicineModelName.Text = procureCatalog.MedicineModelName;
                    txtOutLook.Text = procureCatalog.Outlookc;
                    txtSortSource.Text = HZ.Supervise.Web.Function.FormatDrugSource(procureCatalog.SortSource);
                    txtProcurePrice.Text = procureCatalog.ProcurePrice.ToString();
                    ViewState["price"] = procureCatalog.ProcurePrice.ToString();
                    txtProductName.Text = procureCatalog.ProductName;
                    txtSort.Text = procureCatalog.SortName;
                    txtUnit.Text = procureCatalog.Unit;
                    break;
                case "2":
                    int totalrecord;
                    HZ.Supervise.IBLL.IBLL_SpecialAuthority sp = HZ.Supervise.BLLFactory.IBLL_SpecialAuthority();
                    DataTable dt = sp.AreaPrice(" and ProcureCatalogID= '" + GID + "'", 1, 0, out totalrecord);
                    txtCompany_SC.Text = dt.Rows[0]["CompanyName_SC"].ToString();
                    txtCompany_TB.Text = dt.Rows[0]["CompanyName_TB"].ToString();
                    txtFactor.Text = dt.Rows[0]["Factor"].ToString();
                    txtGoodsName.Text = dt.Rows[0]["GoodsName"].ToString();
                    txtMedicineModelName.Text = dt.Rows[0]["MedicineModelName"].ToString();
                    txtOutLook.Text = dt.Rows[0]["Outlookc"].ToString();
                    txtSortSource.Text = HZ.Supervise.Web.Function.FormatDrugSource(dt.Rows[0]["SortSource"].ToString());
                    txtProcurePrice.Text = dt.Rows[0]["ProcurePrice"].ToString();
                    ViewState["price"] = dt.Rows[0]["ProcurePrice"].ToString();
                    txtProductName.Text = dt.Rows[0]["ProductName"].ToString();
                    txtSort.Text = dt.Rows[0]["SortName"].ToString();
                    txtUnit.Text = dt.Rows[0]["Unit"].ToString();
                    break;
                default:
                    break;
            }
        }


        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (double.Parse(this.txtAdjustPrice.Text) > double.Parse(this.txtProcurePrice.Text))
            {
                this.Page.RegisterStartupScript("", "<script>alert('调整价格不能高于药品限价')</script>");
                this.txtAdjustPrice.Focus();
                return;
            }
            if (this.HidAreaId.Value.Length <1)
            {
                this.Page.RegisterStartupScript("", "<script>alert('请选择需要调价的区域!')</script>");
                return;
            }
            int count=0;
            string areaId = "";
            if (this.cbkArea.Checked)
            {
                areaId = CurrentAccount.GetUser().Org.Area.AreaID;
            }
            else
            {
                areaId = this.HidAreaId.Value.Substring(0, HidAreaId.Value.Length - 1);
            }
            switch (type)
            {
                case "1":
                    count = sp.InsertAreaPrice(areaId, GID, this.txtAdjustPrice.Text, this.cbkArea.Checked, CurrentAccount.GetUser().UserID);
                    break;

                case "2":
                    count = sp.UpdataAreaPrice(areaId, GID, this.txtAdjustPrice.Text, this.cbkArea.Checked, CurrentAccount.GetUser().UserID);
                    break;

                default:
                    break;
            }
            if (count>0)
            {
                this.Page.RegisterStartupScript("", "<script>alert('调整价格成功!')</script>");
            }
            else if (count==-1)
            {
                this.Page.RegisterStartupScript("", "<script>alert('已添加重复调价!')</script>");
            }
            else
            {
                this.Page.RegisterStartupScript("", "<script>alert('调整价格失败!请重新调整..')</script>");
            }

        }

        protected void cbkArea_CheckedChanged(object sender, EventArgs e)
        {
            this.btnArea.Enabled = false;
        }
    }
}
